package dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import entity.Admin;
import util.DBUtil;

public class AdminDao implements Serializable {

	public Admin findByCode(String adminCode) {

		Connection conn = null;
		try {
			conn = DBUtil.getConnection();
			String sql = "select *from admin_info " + "where admin_code=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, adminCode);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				// 创建Admin对象
				Admin admin = new Admin();
				admin.setAdminId(rs.getInt("admin_id"));
				admin.setAdminCode(adminCode);
				admin.setPassword(rs.getString("password"));
				admin.setName(rs.getString("name"));
				admin.setTelephone(rs.getString("telephone"));
				admin.setEmail(rs.getString("email"));
				admin.setEnrolldate(rs.getTimestamp("enrolldate"));
				return admin;
			}

		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("查询管理员失败", e);
		} finally {
			DBUtil.close(conn);
		}
		return null;
	}
	
	public static void main(String[] args) {
		
		AdminDao dao = new AdminDao();
		Admin aa = dao.findByCode("zhangfei");
		System.out.println(aa.getName()+","+aa.getAdminCode()+","+aa.getEmail());
		
	}
	
	

}
